package com.xiaoyu.cloud.config;

import com.xiaoyu.cloud.filter.TransmittableThreadLocalRequestHeadersRemoveFilter;
import com.xiaoyu.common.core.constant.ComConst;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;

@Configuration
public class TransmittableThreadLocalRequestHeadersRemoveConfig {
    //交给spring
    @Bean
    public Filter transmittableThreadLocalRequestHeadersRemoveFilter() {
        return new TransmittableThreadLocalRequestHeadersRemoveFilter();
    }
    @Bean
    public FilterRegistrationBean transmittableThreadLocalRequestHeadersRemoveFilterRegistration() {
        //新建过滤器注册类
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 添加我们写好的过滤器
        //DelegatingFilterProxy就是一个对于servlet filter的代理，用这个类的好处主要是通过Spring容器来管理servlet filter的生命周期，还有就是如果filter中需要一些Spring容器的实例，可以通过spring直接注入，另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现。
        registration.setFilter(new DelegatingFilterProxy("transmittableThreadLocalRequestHeadersRemoveFilter"));
        registration.setName("transmittableThreadLocalRequestHeadersRemoveFilter");
        // 设置过滤器的URL模式
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        //控制过滤器加载顺序：数字越小，加载越早
        registration.setOrder(0);
        return registration;
    }




}
